package com.financial.service.integralpay.service.impl;

import com.financial.service.integralpay.config.UrlConstants;
import com.financial.service.integralpay.connector.FinancialPlatformPayFegin;
import com.financial.service.integralpay.dao.FinancialIntegralRefundReceiptDao;
import com.financial.service.integralpay.domain.FinancialIntegralRefundReceiptDO;
import com.financial.service.integralpay.service.FinancialIntegralRefundService;
import com.financial.service.integralpay.utils.apiresult.ApiResult;
import com.financial.service.integralpay.utils.balanceutils.HttpClientUtils;
import com.financial.service.integralpay.utils.balanceutils.JsonUtils;
import com.financial.service.integralpay.utils.balanceutils.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class FinancialIntegralRefundServiceImpl implements FinancialIntegralRefundService {
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HttpClientUtils httpClientUtils;
    @Autowired
    private FinancialPlatformPayFegin platformPayFegin;
    @Autowired
    private FinancialIntegralRefundReceiptDao gyintegralRefundReceiptDao;

    public static final Logger logger = LoggerFactory.getLogger(FinancialIntegralRefundServiceImpl.class);
    @Override
    public Map<String, Object> refund(Map<String, Object> requestMap) {

            logger.info("financial积分退款接收参数"+ JsonUtils.objectToJson(requestMap));

            String result = null;
            //会员id
            String customerId = requestMap.get("customerId").toString();
            //支付积分
            String backCurrency = requestMap.get("backCurrency").toString();
            //支付单号
            String prePayNo = requestMap.get("prePayNo").toString();
            //token
            String Authorization = requestMap.get("Authorization").toString();
            String platfromMarking = requestMap.get("platfromMarking").toString();
            String payId = requestMap.get("payId").toString();
            if (StringUtils.isEmpty(customerId)||StringUtils.isEmpty(backCurrency)
                    ||StringUtils.isEmpty(prePayNo)||StringUtils.isEmpty(Authorization)
                    ||StringUtils.isEmpty(payId)){
                return ApiResult.paramIsNull();
            }

            try {
                Map<String, String> omap = new HashMap<>();
                omap.put("Authorization",Authorization);
                logger.info("integral--------拼接请求参数对接够样积分平台请求地址:{},对接参数:{},携带token:{}", UrlConstants.INTEGRAL_REFUND_URL, JsonUtils.objectToJson(requestMap),omap);
                result = httpClientUtils.doPostToJson(UrlConstants.INTEGRAL_REFUND_URL,requestMap,omap);
                logger.info("++++customerUuid:{},积分退款信息结果:{}",customerId,result);
                int platfromId = platformPayFegin.getPlatfromId(platfromMarking);
                //请求够样积分退款回执
                Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
                String returnCode = resultMap.get("return_code").toString();
                String message = resultMap.get("message").toString();
                FinancialIntegralRefundReceiptDO gyRefund = new FinancialIntegralRefundReceiptDO();
                gyRefund.setCustomerUuid(customerId);
                gyRefund.setGmtCreate(new Date());
                gyRefund.setGmtModified(new Date());
                gyRefund.setOutTradeNo(prePayNo);
                gyRefund.setIsDelete("1");
                gyRefund.setPlatfromAppid(platfromId);
                gyRefund.setRefundAmount(new BigDecimal(backCurrency));
                gyRefund.setReturnCode(returnCode);
                gyRefund.setReturnMsg(message);
                gyintegralRefundReceiptDao.save(gyRefund);
            } catch (NullPointerException e) {
                e.printStackTrace();
                logger.error("++++customerId:{},积分退款保存失败原因:{}",customerId,e.getMessage());
            }catch (Exception e3){
                e3.printStackTrace();
                logger.error("++++customerId:{},调用够样积分退款失败原因:{}",customerId,e3.getMessage());
            }
            return JsonUtils.jsonToMap(result);
        }

}
